/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * Main game scene
 */

#include "GameScene.h"
#include "GameCamera.h"
#include "GameEngine.h"
#include "layers/LayerBackground.h"
#include "layers/LayerGame.h"
#include "layers/LayerInterface.h"
#include "../MainEngine.h"
#include "../Common.h"
#include "../Resources.h"
#include "../misc/Dialog.h"
#include "../misc/UserData.h"

using namespace cocos2d;

// default constructor
GameScene::GameScene( MainEngine *mainEngine, int level ) : CCScene(), mMaster( mainEngine ), mCurrentLevel( level ), mDialog( 0 ) {
	initScene();
}

// default destructor
GameScene::~GameScene() {
	// TODO: clean up game
}

// initialize this scene
void GameScene::initScene() {
	// create the parallax node for layers
	mParallaxNode = CCParallaxNode::node();
	mParallaxNode->setContentSize( CCSize( WIN_WIDTH, WIN_HEIGHT ) );
	mParallaxNode->setAnchorPoint( ccp( 0.5f, 0.5f ) );
	mParallaxNode->setPosition( WIN_WIDTH / 2.0f, WIN_HEIGHT / 2.0f );

	// create camera
	mGameCamera = new GameCamera( mParallaxNode );

	// create background layer
	mBackgroundLayer = new LayerBackground( this );
	// create game layer
	mGameLayer = new LayerGame( this, mGameCamera );

	// add items to parallax node
	mParallaxNode->addChild( mBackgroundLayer, Z_LAYER_BACKGROUND, ccp( 0.5f, 0.25f ), ccp( WIN_WIDTH / 4.0f, WIN_HEIGHT * 0.39f ) ); 
	mParallaxNode->addChild( mGameLayer, Z_LAYER_GAME_BACK, ccp( 1.0f, 1.0f ), ccp( 0, 0 ) ); 

	// add parallax node as a child
	addChild( mParallaxNode );

	// create interface
	mInterfaceLayer = new LayerInterface( this );
	addChild( mInterfaceLayer );

	// start updating this scene
	scheduleUpdate();
}

// update -function
void GameScene::update( cocos2d::ccTime dt ) {
	// update layers
	mGameCamera->update( dt );
	mBackgroundLayer->update( dt );
	mGameLayer->update( dt );
	mInterfaceLayer->update( dt );

	// possible dialog
	if( mDialog != 0 )
		mDialog->update( dt );
}
	
// get main engine
MainEngine *GameScene::getMaster() {
	return mMaster;
}

// get game layer
LayerGame *GameScene::getGameLayer() {
	return mGameLayer;
}

// get interface layer
LayerInterface *GameScene::getInterfaceLayer() {
	return mInterfaceLayer;
}

// get level
char *GameScene::getCurrentLevel() {
	return Levels::LEVEL[ mCurrentLevel ];
}

// show level cleared dialog
void GameScene::showLevelClearedDialog() {
	// TODO: reward should depend on level instead of a constant!!!
	mMaster->getUserData()->getGameData()->currentMoney = mGameLayer->getGameEngine()->getCurrentMoney() + 2000;
	mMaster->getUserData()->getGameData()->starsCollected[ mCurrentLevel - 1 ] = 1;

	// create dialog
	mDialog = new Dialog( "Level cleared!" );

	// fill with items
	mDialog->addSprite( getResource( Textures::MENU_UNIT_WIN ), WIN_WIDTH * 0.4f, WIN_HEIGHT * 0.39f, CCSize( 0.65f, 0.65f ) );
	if( mCurrentLevel != 10 )
		mDialog->addTextButton( this, menu_selector( GameScene::nextLevelCallback ), "Next Level", WIN_WIDTH * 0.58f, WIN_HEIGHT * 0.49f, CCSize( 0.7f, 0.7f ) );
	mDialog->addTextButton( this, menu_selector( GameScene::restartCallback ), "Restart", WIN_WIDTH * 0.58f, WIN_HEIGHT * 0.37f, CCSize( 0.7f, 0.7f ) );
	mDialog->addTextButton( this, menu_selector( GameScene::backToMenuCallback ), "Exit", WIN_WIDTH * 0.58f, WIN_HEIGHT * 0.25f, CCSize( 0.7f, 0.7f ) );

	// add as child
	addChild( mDialog, Z_LAYER_MENU );
	mDialog->showDialogFadeIn();
}

// show level failed dialog
void GameScene::showLevelFailedDialog() {
	// create dialog
	mDialog = new Dialog( "Level failed!" );

	// fill with items
	mDialog->addSprite( getResource( Textures::MENU_UNIT_LOSE ), WIN_WIDTH * 0.4f, WIN_HEIGHT * 0.39f, CCSize( 0.65f, 0.65f ) );
	mDialog->addTextButton( this, menu_selector( GameScene::restartCallback ), "Restart", WIN_WIDTH * 0.58f, WIN_HEIGHT * 0.43f, CCSize( 0.7f, 0.7f ) );
	mDialog->addTextButton( this, menu_selector( GameScene::backToMenuCallback ), "Exit", WIN_WIDTH * 0.58f, WIN_HEIGHT * 0.31f, CCSize( 0.7f, 0.7f ) );

	// add as child
	addChild( mDialog, Z_LAYER_MENU );
	mDialog->showDialogFadeIn();
}

// callback functions for dialog

void GameScene::restartCallback( cocos2d::CCObject *pSender ) {
	cocos2d::CCScene *nextLevelScene = new GameScene( mMaster, mCurrentLevel );
	mMaster->sceneFinishedExecution( nextLevelScene, SCENE_GAME );
}

void GameScene::backToMenuCallback( cocos2d::CCObject *pSender ) {
	mMaster->sceneFinishedExecution();
}

void GameScene::nextLevelCallback( cocos2d::CCObject *pSender ) {
	cocos2d::CCScene *nextLevelScene = new GameScene( mMaster, mCurrentLevel + 1 );
	mMaster->sceneFinishedExecution( nextLevelScene, SCENE_GAME );
}

// get background layer
LayerBackground *GameScene::getLayerBackground() {
	return( mBackgroundLayer );
}
